package com.aigis.big4.mapreduce;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

import java.io.IOException;
import java.util.StringTokenizer;

/**
 * 资料
 * https://blog.csdn.net/u012116457/article/details/55211768
 */
public class MultiFileCount {

    public static class StringCountMapper extends Mapper<Object,Text,Text,Text> {
        private static final String word="月";
        private FileSplit fileSplit;

        protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            fileSplit= (FileSplit) context.getInputSplit();
            String fileName=fileSplit.getPath().getName().toString();
            StringTokenizer st=new StringTokenizer(value.toString(),"。");
            while (st.hasMoreTokens()){
              String line= st.nextToken().toString();
              if (line.indexOf(word)>=0){
                 context.write(new Text(fileName),new Text(line));
              }
            }
        }
    }

    public static class StringReducer extends Reducer<Text,Text,Text,Text>{
        @Override
        public void reduce(Text key,Iterable<Text> values,Context context) throws IOException, InterruptedException{
            String lines = "";
            for(Text value:values){
                lines += value.toString()+"---|---";
            }
            context.write(key, new Text(lines));
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        args=new String[]{"hdfs://127.0.0.1:9000/test/multiFileCount/input","hdfs://127.0.0.1:9000/test/multiFileCount/output"};
        Configuration configuration=new Configuration();
        String[] otherArgs=new GenericOptionsParser(configuration,args).getRemainingArgs();
        if (otherArgs.length<2){
            System.out.println("Usage：inFolder,outFolder");
            System.exit(1);
        }

        configuration.set("mapred.job.tracker","hdfs://127.0.0.1:9001");
        configuration.set("fs.default.name","hdfs://127.0.0.1:9000");
        Job job=Job.getInstance(configuration,"MultiFileStatic");
        job.setJarByClass(MultiFileCount.class);
        job.setMapperClass(MultiFileCount.StringCountMapper.class);
        job.setCombinerClass(MultiFileCount.StringReducer.class);
        job.setReducerClass(MultiFileCount.StringReducer.class);
        //job.setInputFormatClass();
        FileInputFormat.addInputPath(job,new Path(otherArgs[0]));
        FileOutputFormat.setOutputPath(job,new Path(otherArgs[1]));
        boolean flag=job.waitForCompletion(true);
        System.exit(flag ? 0:1);
    }

}
